local Loveframes = require"library/loveframes"

local start = class("startScene",require"game/scene"()) {
    ui = nil;
    window_width = 350;
    window_height = 110;
}

function start:_init()
    self.ui = Loveframes();
    self.name = "Start";
    self:_initUI()
end

function start:_enter()
    love.window.setMode(self.window_width,self.window_height);
end

function start:_initUI()
    for _, skin in pairs(self.ui.skins) do
        skin.controls.smallfont = Game.FONT;
        skin.controls.imagebuttonfont = Game.FONT;
    end

    local network = Game.Network;
    local panel_offset = 5

    self.panel_a_x = 0
    self.panel_a_y = 0
    self.panel_a_w = self.window_width
    self.panel_a_h = self.window_height * 0.25

    self.panel_b_x = self.panel_a_x
    self.panel_b_y = self.panel_a_y + self.panel_a_h + panel_offset
    self.panel_b_w = self.panel_a_w
    self.panel_b_h = self.window_height - self.panel_a_h - panel_offset

    self.panel_a = self.ui.Create("panel")
        :SetPos(self.panel_a_x,self.panel_a_y)
        :SetSize(self.panel_a_w,self.panel_a_h)

    self.panel_a_item_a_x = 5
    self.panel_a_item_a_label = "你的名字"
    self.panel_a_item_a_label_w = Game.FONT:getWidth(self.panel_a_item_a_label)
    self.panel_a_item_a = self.ui.Create("text",self.panel_a)
        :SetFont(Game.FONT)     
        :SetText(self.panel_a_item_a_label)
        :SetPos(self.panel_a_item_a_x,nil)
        :CenterY();   
    
    self.panel_a_item_b_x = self.panel_a_item_a_x+self.panel_a_item_a_label_w + 50
    self.panel_a_item_b = self.ui.Create("textinput",self.panel_a)
        :SetFont(Game.FONT)    
        :SetPos(self.panel_a_item_b_x,nil)
        :SetSize(self.panel_a_w - self.panel_a_item_b_x,self.panel_a_h)
        :SetText("老红")
        :CenterY()

    self.panel_b_button_number = 1
    self.panel_b_button_w = self.panel_b_w
    self.panel_b_button_h = (self.panel_b_h * 0.9) / self.panel_b_button_number

    self.panel_b = self.ui.Create("grid",self.panel_b)
        :SetSize(self.panel_b_w,self.panel_b_h)
        :SetPos(self.panel_b_x,self.panel_b_y)
        :SetRows(2)
        :SetColumns(1)
        :SetCellPadding(0)
        :SetCellWidth(self.panel_b_button_w)
        :SetCellHeight(self.panel_b_button_h)
        :SetItemAutoSize(true)
        ;
   
    self.enter_game_button = self.ui.Create("button",self.panel_b_grid)
        :SetText("进入游戏")
    self.enter_game_button.OnClick = function()
        if Game.Network.Sock:getState() == "connected" then
            Game.Network.name = self.panel_a_item_b:GetText()
            self.enter_game_button.clickable = false;
            Game.Network.Sock:send("JoinServer",{name = Game.Network.name})
        else
            self.enter_game_button.clickable = true;
        end
    end
    self.panel_b:AddItem(self.enter_game_button,1,1)

    network:addSigbalConnect("JoinServerSucceed",self,self._JoinServerSucceed)
    network:addSigbalConnect("JoinServerFail",self,self._JoinServerFail)
end

function start:_JoinServerSucceed()
    Game:switchScene("Room")
    self.enter_game_button.clickable = true;
end

function start:_JoinServerFail()
    self.enter_game_button.clickable = true;
end

function start:_draw()self.ui:draw() end
function start:_update(dt) self.ui.update(dt) end
function start:_mousepressed(x, y, button) self.ui.mousepressed(x, y, button) end
function start:_mousereleased(x, y, button) self.ui.mousereleased(x, y, button) end
function start:_wheelmoved(x, y) self.ui.wheelmoved(x, y) end
function start:_keypressed(k, scancode, isrepeat) self.ui.keypressed(k, isrepeat) end
function start:_keyreleased(key) self.ui.keyreleased(key) end
function start:_textinput(text) self.ui.textinput(text) end

return start

